package com.es.disped.web.controller.clazzstudent;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.clazzstudent.ClazzStudentService;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.ClazzForeignModel;
import com.es.disped.dao.model.ClazzStudent;
import com.es.disped.dao.model.ClazzStudentExample;
import com.es.disped.dao.model.UserForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月6日 下午8:33:32<br>
 *  名称：ClazzStudentController.java<br>
 *  描述：班级学生管理<br>
 */
@Api(value="班级学生关系管理")
@Controller
@RequestMapping("/manage/clazz/student")
public class ClazzStudentController extends BaseController{
	
	private final String CLAZZ_NAMESPACE="com.es.disped.dao.mapper.clazz.ClazzCustomMapper";
	
	private final String USER_NAMESPACE="com.es.disped.dao.mapper.user.UserCustomMapper";
	
	@Autowired
	ClazzStudentService clazzStudentService;
	
	@ApiOperation(value="保存班级学生关系对象",response=MsgModel.class,httpMethod="POST",notes="保存班级学生关系对象")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="clazzStudent",value="班级学生对象",required=true) ClazzStudent clazzStudent)
	{
		int count=0;
		if(Optional.of(clazzStudent)
				.filter(cId->clazzStudent.getClazzId()!=null)
				.filter(sId->clazzStudent.getStudentId()!=null)
				.isPresent())
		{
			ClazzForeignModel clazzForeignModel=this.getService().selectOne(CLAZZ_NAMESPACE+".getClazzForeignModel", clazzStudent.getClazzId());
			if(clazzForeignModel.getClazzStudentNum()>clazzForeignModel.getClazzMaxStudent())
			{
				clazzStudent.setClazzName(clazzForeignModel.getClazzName());
				clazzStudent.setRelId(getUUID());
				UserForeignModel userForeignModel=this.getService().selectOne(USER_NAMESPACE+".getUserForeignModel", clazzStudent.getStudentId());
				clazzStudent.setStudentName(userForeignModel.getUserName());
				count=clazzStudentService.insertSelective(clazzStudent);
			}
			//若学生添加成功，则更新班级人数
			if(count==1)
			{
				this.getService().update(CLAZZ_NAMESPACE+".addClazzStudentNum", clazzStudent.getClazzId());
			}
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败", count==1?clazzStudent:"");
	}
	
	@ApiOperation(value="更新班级学生关系对象",response=MsgModel.class,httpMethod="POST",notes="更新班级学生关系对象")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="clazzStudent",value="班级学生对象",required=true) ClazzStudent clazzStudent)
	{
		int count=0;
		if(!this.isNull(clazzStudent.getRelId()))
		{
			if(!this.isNull(clazzStudent.getClazzId()))
			{
				ClazzForeignModel clazzForeignModel=this.getService().selectOne(CLAZZ_NAMESPACE+".getClazzForeignModel", clazzStudent.getClazzId());
				clazzStudent.setClazzName(clazzForeignModel.getClazzName());
			}
			if(!this.isNull(clazzStudent.getStudentId()))
			{
				UserForeignModel userForeignModel=this.getService().selectOne(USER_NAMESPACE+".getUserForeignModel", clazzStudent.getStudentId());
				clazzStudent.setStudentName(userForeignModel.getUserName());
			}
			count=clazzStudentService.updateByPrimaryKeySelective(clazzStudent);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"更新成功":"更新失败", count==1?clazzStudent:"");
	}
	
	@ApiOperation(value="删除班级学生关系对象",response=MsgModel.class,httpMethod="POST",notes="删除班级学生关系对象")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="clazzStudent",value="班级学生对象",required=true) ClazzStudent clazzStudent)
	{
		int count=0;
		if(!this.isNull(clazzStudent.getRelId()))
		{
			count=clazzStudentService.deleteByPrimaryKey(clazzStudent.getRelId());
			this.getService().update(CLAZZ_NAMESPACE+".minusClazzStudentNum", clazzStudent.getClazzId());
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"删除成功":"删除失败", count==1?clazzStudent:"");
	}
	
	
	@ApiOperation(value="检索班级学生关系对象",response=MsgModel.class,httpMethod="POST",
			notes="检索班级学生关系对象，精确检索：关系主键ID，班级ID，学生ID；模糊检索：班级名称，学生姓名")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="clazzStudent",value="班级学生对象",required=false) ClazzStudent clazzStudent)
	{
		ClazzStudentExample listExample=new ClazzStudentExample();
		ClazzStudentExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(clazzStudent.getRelId()))
		{
			criteria.andRelIdEqualTo(clazzStudent.getRelId());
		}
		if(!this.isNull(clazzStudent.getClazzId()))
		{
			criteria.andClazzIdEqualTo(clazzStudent.getClazzId());
		}
		if(!this.isNull(clazzStudent.getStudentId()))
		{
			criteria.andStudentIdEqualTo(clazzStudent.getStudentId());
		}
		if(!this.isNull(clazzStudent.getClazzName()))
		{
			criteria.andClazzNameLike("%"+clazzStudent.getClazzName()+"%");
		}
		if(!this.isNull(clazzStudent.getStudentName()))
		{
			criteria.andStudentNameLike("%"+clazzStudent.getStudentName()+"%");
		}
		List<ClazzStudent> list=clazzStudentService.selectByExample(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"班级学生加载成功":"班级学生加载失败", !this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载班级学生",response=MsgModel.class,httpMethod="POST",notes="分页加载班级学生，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		ClazzStudentExample listExample=new ClazzStudentExample();
		PageModel<ClazzStudent> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=clazzStudentService.selectByExampleForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"班级学生加载成功":"班级学生加载失败",!this.isNull(pageModel)?pageModel:"");
	}
	
}
